using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace sCicero
{
    public class FileHandler : Base
    {
        private const string FILESLINK = "fileslink_";
        private const string FILES = "files";
        DataTable m_oFilesTable;

        public FileHandler(ref Data oData)
        {
            m_oData = oData;
            m_Table = FILES;
            m_oFilesTable = new DataTable("FilesGeneralView");
            
            DataColumn oFileNameTableColumn = new DataColumn();
            oFileNameTableColumn.DataType = System.Type.GetType("System.String");
            oFileNameTableColumn.ColumnName = "filename";
            m_oFilesTable.Columns.Add(oFileNameTableColumn);

            DataColumn oFileNameOrigTableColumn = new DataColumn();
            oFileNameOrigTableColumn.DataType = System.Type.GetType("System.String");
            oFileNameOrigTableColumn.ColumnName = "origfilename";
            m_oFilesTable.Columns.Add(oFileNameOrigTableColumn);

            DataColumn oFileIdTableColumn = new DataColumn();
            oFileIdTableColumn.DataType = System.Type.GetType("System.Int32");
            oFileIdTableColumn.ColumnName = "file_id";
            m_oFilesTable.Columns.Add(oFileIdTableColumn);
        }

        public DataTable GetFiles(string sTable, int nBaseId, string sColumn)
        {
            if (m_oFilesTable.Rows.Count > 0)
                m_oFilesTable.Rows.Clear();

            foreach (DataRow oDr in m_oData.GetGenericDataTable(FILESLINK + sTable).Select(sColumn + " = " + nBaseId.ToString()))
            {
                foreach (DataRow oDr_file in m_oData.GetGenericDataTable("files").Select("id = " + oDr["file_id"].ToString()))
                {
                    DataRow oNewRow = m_oFilesTable.NewRow();
                    oNewRow["origfilename"] = oDr_file["origfilename"].ToString();
                    oNewRow["filename"] = oDr_file["filename"].ToString();
                    oNewRow["file_id"] = oDr_file["id"];
                    m_oFilesTable.Rows.Add(oNewRow);
                }
            }
            return m_oFilesTable;
            
        }
        public DataTable GetFiles(string sTable, int nBaseId)
        {
            if (m_oFilesTable.Rows.Count > 0)
                m_oFilesTable.Rows.Clear();

            foreach (DataRow oDr in m_oData.GetGenericDataTable(FILESLINK + sTable).Select(sTable + "_id = " + nBaseId.ToString()))
            {
                foreach (DataRow oDr_file in m_oData.GetGenericDataTable("files").Select("id = " + oDr["file_id"].ToString()))
                {
                    DataRow oNewRow = m_oFilesTable.NewRow();
                    oNewRow["origfilename"] = oDr_file["origfilename"].ToString();
                    oNewRow["filename"] = oDr_file["filename"].ToString();
                    oNewRow["file_id"] = oDr_file["id"];
                    m_oFilesTable.Rows.Add(oNewRow);
                }
            }
            return m_oFilesTable;
        }

        public string GetDecryptedFileName(int nFileId)
        {

            //string sFile = m_oData.Get(nFileId)["filename"].ToString();
            string sFile = m_oData.GetGenericDataTable(FILES).Select("id = " + nFileId.ToString())[0]["filename"].ToString();
            string sFileSaved = m_oData.GetGenericDataTable(FILES).Select("id = " + nFileId.ToString())[0]["origfilename"].ToString();
            System.IO.FileInfo oF = new System.IO.FileInfo(sFileSaved);
            cFile oFile = new cFile();
            Errorcode oE = oFile.SaveDecrypted("filedata\\" + sFile, "tmp\\" + oF.Name);
            if (oE == Errorcode.NOERROR)
                return @"tmp\" + oF.Name;
            else
                return "";
            //Errorcode oE_file = oFile.Save(sOrigFileName, "filedata\\" + ts.TotalSeconds.ToString(), false);
        }

    }
}
